namespace Collaboris.Saf.Actions.Wss.Tests.Generated
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI.WebControls.WebParts;

    using Engine.Report;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Portal.WebControls;
    using Microsoft.SharePoint.WebPartPages;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Saf.Tests;
    using Entities;

    using SharePoint.Wss.Files;
    using SharePoint.Wss.Lists;
    using SharePoint.Wss.WebParts;

    using Wss.WebPart;

    using WebPart=Microsoft.SharePoint.WebPartPages.WebPart;

    /// <summary>
    /// This test was originally generated but has now been tested and verfiied by MJ
    /// </summary
    [TestClass]
    public class ConnectWebPartsTest
    {
        [TestMethod]
        public void ConnectWebParts_Test()
        {
            WSSTestAssistant<ConnectWebParts, WebPartConnectionInfo> connectwebparts =
                    new WSSTestAssistant<ConnectWebParts, WebPartConnectionInfo>(TestEnvironment.PublishingSite, 
                        "ConnectWebPartsTestWeb", "BLANKINTERNET#0");

            using (connectwebparts)
            {
                SPSlicerTextWebPart providerWebPart = new SPSlicerTextWebPart();
                providerWebPart.ID = "ProviderWebPart1";
                providerWebPart.Title = "provider webpart";
                providerWebPart.FilterName = "Some Filter name";

                SPList list = SPListHelper.GetList(connectwebparts.TestWeb, "Pages");

                ListViewWebPart consumerWebPart = new ListViewWebPart();
                consumerWebPart.ID = "ConsumerWebPart1";
                consumerWebPart.Title = "consumer webpart";
                consumerWebPart.ListName = list.ID.ToString("B").ToUpper();
                consumerWebPart.ViewGuid = list.DefaultView.ID.ToString("B").ToUpper();
                        

                SPFile defaultAspx = SPFileHelper.GetFile(connectwebparts.TestWeb, "Pages", "Default.aspx");
                defaultAspx.CheckOut();

                List<WebPart> webParts = new List<WebPart>() { providerWebPart, consumerWebPart };

                SPWebPartHelper.AddWebParts(connectwebparts.TestWeb, defaultAspx, webParts, "MiddleColumn", true, PersonalizationScope.Shared);

                // now finally lets go and get these 2 web parts up!
                MacroReport doReport = connectwebparts.DoMacro();


                WebPart webPart = (WebPart)SPWebPartHelper.GetWebPartFromPage(
                                               connectwebparts.TestWeb,
                                               String.Format(@"/{0}/{1}", "Pages", "Default.aspx"),
                                               consumerWebPart.ID,
                                               PersonalizationScope.Shared);

                List<SPWebPartConnection> consumerConnections;
                List<SPWebPartConnection> providerConnections;
                SPWebPartHelper.GetConnectionsForWebPart(connectwebparts.TestWeb, 
                                                    "Pages/Default.aspx", 
                                                    PersonalizationScope.Shared, 
                                                    consumerWebPart.ID, 
                                                    out consumerConnections, 
                                                    out providerConnections);

                Assert.IsTrue(consumerConnections[0].ConsumerID == connectwebparts.FirstEntity.ConsumerWebPartId);
                Assert.IsTrue(consumerConnections[0].ProviderID == connectwebparts.FirstEntity.ProviderWebPartId);
                Assert.IsTrue(providerConnections.Count == 0);

                List<SPWebPartConnection> consumerConnections1;
                List<SPWebPartConnection> providerConnections1;
                SPWebPartHelper.GetConnectionsForWebPart(connectwebparts.TestWeb,
                                                    "Pages/Default.aspx",
                                                    PersonalizationScope.Shared,
                                                    providerWebPart.ID,
                                                    out consumerConnections1,
                                                    out providerConnections1);

                Assert.IsTrue(providerConnections1[0].ConsumerID == connectwebparts.FirstEntity.ConsumerWebPartId);
                Assert.IsTrue(providerConnections1[0].ProviderID == connectwebparts.FirstEntity.ProviderWebPartId);
                Assert.IsTrue(consumerConnections1.Count == 0);




            }
        }

    }
}
